package com.bolingcavalry.crm.serviceproblem.controller;/*
 * @Auther:Sadie
 * @Date:2025/5/19
 * @Description:
 * @VERSON:1.8
 */

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.base.PageResult;
import com.base.ResponseResult;
import com.bolingcavalry.crm.serviceproblem.service.ServiceProblemService;
import com.dto.serviceproblem.ServiceProblemCopyDTO;
import com.dto.serviceproblem.ServiceProblemDTO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.vo.ServiceProblemCountVO;
import com.vo.ServiceProblemStatusCountVO;
import com.vo.ServiceProblemVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/serviceproblem")
@Slf4j
@Api("服务问题管理")
public class ServiceproblemController {
    @Autowired
    private ServiceProblemService serviceProblemService;

    //新增服务问题
    @PostMapping("/add")
    public ResponseResult add(@RequestBody ServiceProblemDTO serviceProblemDTO) {
        return serviceProblemService.add(serviceProblemDTO);
    }

    //修改服务问题
    @PostMapping("/update")
    public ResponseResult update(@RequestBody ServiceProblemDTO serviceProblemDTO) {
        serviceProblemService.update(serviceProblemDTO);
        return ResponseResult.success("修改新增问题成功");
    }

    //更新状态
    @GetMapping("/updateStatus")
    public ResponseResult updateStatus(
            @RequestParam Long id,
            @RequestParam Integer status) {
        if(serviceProblemService.updateStatus(id, status)){
            return ResponseResult.success("更新状态成功");
        }
        return ResponseResult.success("更新状态失败");
    }

    //逻辑批量删除服务问题
    @DeleteMapping("/batchdelete")
    public ResponseResult<String> batchDelete(@RequestParam Long[] ids) {
        List<Long> failedIds = serviceProblemService.batchDelete(ids);
        if (failedIds.isEmpty()) {
            return ResponseResult.success("批量逻辑删除成功");
        } else {
            return ResponseResult.error("部分ID删除失败", failedIds);
        }
    }

    //获取问题详细
    @GetMapping("/get/{id}")
    public ResponseResult get(@PathVariable Long id) throws JsonProcessingException {
        ServiceProblemVO serviceProblemVO = serviceProblemService.getById(id);
        if(serviceProblemVO == null){
            return ResponseResult.error("未找到ID为 " + id + " 的服务问题");
        }
        return ResponseResult.success("获取服务问题成功", serviceProblemVO);
    }

    //分页条件查询服务问题
    @GetMapping("/list")
    public ResponseResult<IPage<ServiceProblemVO>> list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String OrderNumber, // 售后单号
            @RequestParam(required = false) String installStation,  // 安装站名
            @RequestParam(required = false) String installationLocation, // 安装位置
            @RequestParam(required = false) String boreSize,        // 口径
            @RequestParam(required = false) String modelNumber,     // 型号
            @RequestParam(required = false) String productionNumber,// 出厂编号
            @RequestParam(required = false) String problemCategory, // 问题归类
            @RequestParam(required = false) String startDate,       // 开始时间
            @RequestParam(required = false) String endDate,         // 结束时间
            @RequestParam(required = false) Integer status,          // 状态
            @RequestParam(required = false) String customerCompanyName, // 客户公司名称
            @RequestParam(required = false) String faultDescription, // 故障现象
            @RequestParam(required = false) String createdBy,
            @RequestParam(required = false) String keyword //关联字（搜索框）
    ){
        IPage<ServiceProblemVO> page = serviceProblemService.list(pageNum, pageSize,
                OrderNumber, installStation, installationLocation,
                boreSize, modelNumber, productionNumber, problemCategory,
                startDate, endDate, status, customerCompanyName,faultDescription,keyword,createdBy);
        PageResult<ServiceProblemVO> result = new PageResult<>();
        result.setTotal(page.getTotal());
        result.setPages((int) page.getPages());
        result.setCurrent((int) page.getCurrent());
        result.setSize((int) page.getSize());
        result.setRecords(page.getRecords());
        return ResponseResult.success("获取服务问题成功", result);
    }

    //获取服务问题状态统计（本月新增服务问题、全部服务问题）
    @GetMapping("/count")
    public ResponseResult<ServiceProblemStatusCountVO> count() {
        ServiceProblemStatusCountVO serviceProblemcountVO = serviceProblemService.getCount();
        return ResponseResult.success("获取服务问题统计成功", serviceProblemcountVO);
    }

    //获取故障归类下问题归类的数量统计
    @GetMapping("/problemCategoryCount")
    public ResponseResult<List<ServiceProblemCountVO>> problemCategoryCount() {
        List<ServiceProblemCountVO> serviceProblemVOList = serviceProblemService.problemCategoryCount();
        return ResponseResult.success("获取服务问题统计成功", serviceProblemVOList);
    }

    //复制接口，返回一个拼接好的问题详细返回
    @PostMapping("/CopyByIds")
    public ResponseResult<String> CopyById(@RequestBody  ServiceProblemCopyDTO copyDTO) {
        List<Long> ids = copyDTO.getIds();
        return serviceProblemService.getCopyByIds(ids);
    }


}

